package com.baicai.qqlogindemo;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.widget.CompoundButton;
import android.widget.Switch;

/**
 * Create By：bai_cai，on 2019/1/12
 * God bless my code!
 */
public class PreferenceDemoActivity extends Activity implements CompoundButton.OnCheckedChangeListener {

    private static final String TAG = "PreferenceDemoActivity";
    private Switch mIsAllowUnknow;
    private SharedPreferences mSharedPreferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activety_preference_demo);

        // 找控件
        mIsAllowUnknow = this.findViewById(R.id.is_allow_unknown);
        mIsAllowUnknow.setOnCheckedChangeListener(this);
        mSharedPreferences = this.getSharedPreferences("setting_info", MODE_PRIVATE);
        boolean state = mSharedPreferences.getBoolean("state", false);
        mIsAllowUnknow.setChecked(state);
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        // 保存信息
        Log.d(TAG, "state == " + isChecked);
        SharedPreferences.Editor edit = mSharedPreferences.edit();
        edit.putBoolean("state", isChecked);
        edit.commit();
    }
}
